package com.yykj.tencent.im.server.service.impl;

import com.alibaba.fastjson.JSON;
import com.yykj.tencent.im.client.form.TpnsSendForm;
import com.yykj.tencent.im.server.service.TpnsService;
import com.yykj.tencent.im.server.utils.HttpUtil;
import com.yykj.tencent.im.server.utils.SignUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.HashSet;

@Service
@Slf4j
public class TpnsServiceImpl implements TpnsService {

    public static final String accessID = "1580010139";
    public static final String accessKey = "AXKHTUQC7BRB";
    public static final String secretKey = "691ade043c050f841ca2e4428975e008";

    public static final String ios_accessID = "1680010140";
    public static final String ios_accessKey = "IJ2ED0UP9ELT";
    public static final String ios_secretKey = "087562d8ec4f130b7f53df52ce1f4b58";

    public static final String url = "https://api.tpns.sh.tencent.com/v3/push/app";

    public static void send(String str) {
        long timeStamp = System.currentTimeMillis() / 1000;
        String sign = SignUtil.getSign(timeStamp, str, accessID, secretKey);
        HashMap<String, Object> header = new HashMap<>();
        header.put("Sign", sign);
        header.put("AccessId", accessID);
        header.put("TimeStamp", timeStamp + "");
        try {
            String post = HttpUtil.jsonSend(url, null, header, str);
            System.out.println(post);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void sendIos(String str) {
        long timeStamp = System.currentTimeMillis() / 1000;
        String sign = SignUtil.getSign(timeStamp, str, ios_accessID, ios_secretKey);
        HashMap<String, Object> header = new HashMap<>();
        header.put("Sign", sign);
        header.put("AccessId", ios_accessID);
        header.put("TimeStamp", timeStamp + "");
        try {
            String post = HttpUtil.jsonSend(url, null, header, str);
            System.out.println(post);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public void pushApp(TpnsSendForm tpnsSendForm) {
        HashMap<String, Object> data = new HashMap<>();
        data.put("audience_type", "account_list");
        HashMap<String, Object> message = new HashMap<>();
        message.put("title", tpnsSendForm.getTitle());
        message.put("content", tpnsSendForm.getContent());
        data.put("message", message);
        data.put("account_list", tpnsSendForm.getAccountList());
        data.put("message_type", "notify");
        send(JSON.toJSONString(data));
        pushAppIos(tpnsSendForm);
    }

    public void pushAppIos(TpnsSendForm tpnsSendForm) {
        HashMap<String, Object> data = new HashMap<>();
        data.put("audience_type", "account_list");
        HashMap<String, Object> message = new HashMap<>();
        message.put("title", tpnsSendForm.getTitle());
        message.put("content", tpnsSendForm.getContent());
        HashMap<String, Object> ios = new HashMap<>();
        HashMap<String, Object> aps = new HashMap<>();

        aps.put("alert", tpnsSendForm.getTitle());
        ios.put("aps", aps);
        data.put("ios", ios);
        data.put("message", message);
        data.put("account_list", tpnsSendForm.getAccountList());
        data.put("message_type", "notify");
        data.put("show_type", "1");
        data.put("environment", "dev");
        sendIos(JSON.toJSONString(data));
    }

    public static void main(String[] args) {
        HashSet<String> accountList = new HashSet<>();
        accountList.add("dev86cb631fb6c8465c84396abd42c22703");
        accountList.add("prod1b3d26a116a44ce5ab884e775bbe707f");
        accountList.add("dev5652d182e9784ec09e19c6e68b203eaa");
        TpnsSendForm build = TpnsSendForm.builder()
                .content("消息内容")
                .title("消息标题")
                .accountList(accountList).build();
        new TpnsServiceImpl().pushAppIos(build);
    }
}
